package com.chijun.mform.field.impl;


import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;

import com.chijun.mform.create.mode.FormField;
import com.chijun.mform.field.BaseField;
import com.example.ccj.b.c.custom.MTextView;
import com.example.ccj.u.MoneyUtils;
import com.example.ccj.u.StringUtils;
import com.example.ccj.u.message.UiMessageUtils;
import com.example.ccj.u.text.TextInputType;

import java.math.BigDecimal;

import io.reactivex.ObservableEmitter;

/**
 * 金额
 * @author 驰珺
 * whechat   : chijun9
 * create at : 2024/12/23 15:44
 */
public class FieldFor11JinE extends BaseField {
    public FieldFor11JinE(){
        super();
    }

    @Override
    public View getWritelView(Context context, WriteFormClass writeFormClass) {
        FormField formField = writeFormClass.formField;
        //提示文本
        if (formField.getHint() != null) writeFormClass.fieldFullContentView.setHint(formField.getHint());
        //输入文本颜色
        if (formField.getTextColor() != 0) writeFormClass.fieldFullContentView.setTextColor(formField.getTextColor());
        //是否只读
        if (formField.isReadOnly()) {
            writeFormClass.fieldFullContentView.setKeyListener(null);
            writeFormClass.fieldFullContentView.setFocusable(false);
        }

        //默认文本
        String textFieldZanCunData = getTextFieldZanCunData(context,"fieldId=?",String.valueOf(formField.getId()));
        if (textFieldZanCunData == null) textFieldZanCunData = formField.getDefaultContent();
        if (!StringUtils.isEmpty(textFieldZanCunData)) {
            writeFormClass.fieldFullContentView.setText(textFieldZanCunData);
            saveValue = textFieldZanCunData;
            writeFormClass.inputIsOK = true;
        }

        TextInputType.setEditTextInPutTypeToDouble(writeFormClass.fieldFullContentView,20,writeFormClass.formField.getLittleNum());
        MTextView daXie = new MTextView(context);
        daXie.setTextSize(13);
        daXie.setTextIsSelectable(true);

        if (formField.isShowBig()) {
            writeFormClass.fieldFullContentLayout.setVisibility(View.VISIBLE);
            writeFormClass.fieldFullContentLayout.addView(daXie);
        }

        writeFormClass.fieldFullContentView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            @Override
            public void afterTextChanged(Editable s) {
                //验证是否为空
                if (formField.isMast() || StringUtils.isEmpty(s)) {
                    writeFormClass.fieldFullWriteErrorView.setVisibility(View.VISIBLE);
                    writeFormClass.fieldFullWriteErrorView.setText("该字段不可为空！");
                    return;
                }

                if (formField.isShowBig()) {
                    BigDecimal bigDecimal = new BigDecimal(s.toString());
                    String s1 = MoneyUtils.lowerToUpper(bigDecimal);
                    daXie.setText(s1);
                }
                writeFormClass.fieldFullWriteErrorView.setVisibility(View.GONE);
                //用于保存的值
                saveValue = s;
                //发送消息，用于计算公式。
                UiMessageUtils.getInstance().send(JI_SUAN_GONG_SHI, formField.getId()+","+s);
            }
        });

        return writeFormClass.view;
    }


    @Override
    protected View getShowFullView(Context context, FullClass fullModel) {
        //字段名属性设置
        fullModel.setFieldProperty();
        //设置字段值
        fullModel.setContentProperty();


        TextInputType.setEditTextInPutTypeToDouble(fullModel.loadContent,20,fullModel.formField.getLittleNum());

        MTextView daXie = new MTextView(context);
        daXie.setTextSize(13);
        daXie.setTextIsSelectable(true);
        if (fullModel.formField.isShowBig()) {
            fullModel.addCustomControl(daXie);
        }

        if (fullModel.isHaveModifyPower()) {
            fullModel.loadContent.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {}
                @Override
                public void afterTextChanged(Editable s) {
                    BigDecimal bigDecimal = new BigDecimal(s.toString());
                    String s1 = MoneyUtils.lowerToUpper(bigDecimal);
                    daXie.setText(s1);
                }
            });
        }

        return fullModel.layout;
    }


    /**
     * 字段设置界面
     * @param context 上下文
     * @param installClass 配置
     * @return 1
     */
    @Override
    protected View getFieldInstallView(Context context, InstallClass installClass) {
        ObservableEmitter<ViewGroup> emitter = installClass.emitter;

        emitter.onNext(installClass.getNumberView());        //小数位数设置
        emitter.onNext(installClass.getIsShowBigView());     //是否显示大写金额设置
        emitter.onNext(installClass.getIsMastView());        //是否必填
        emitter.onNext(installClass.getIsPrintView());        //是否参与打印

        return installClass.v;
    }


}
